package games.gobang.controllers
{
	import flash.geom.Point;
	
	import games.gobang.models.vo.GUser;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	import views.GameWindow;

	[Bindable]
	public class GobangLogicCtrller
	{
		public var grids:Vector.<Vector.<int>> = new Vector.<Vector.<int>>(15,true);
		public var opponent:GUser;
		public var myTurn:Boolean;
		public static const WIN:String = "win";
		public static const LOSE:String = "lose";
		public static const DEUCE:String = "deuce";
		public function GobangLogicCtrller()
		{
		}
		private function clearGrids():void{
			for(var i:int;i<15;i++){
				grids[i] = new Vector.<int>(15,true);
				for(var j:int = 0;j<15;j++)
					grids[i][j] = 0;
			}
		}
		private function clearData():void{
			clearGrids();
			GUser.self.clearChess();
			opponent.clearChess();
			myTurn = false;
		}
		[DexterEvent]
		public function getChessman(pos:Point):int{
			return grids[pos.x][pos.y];
		}
		[DexterEvent]
		public function playChess(pos:Point):void{
			myTurn = false;
			grids[pos.x][pos.y] = 1;
			GUser.self.chess.push(pos);
			sendDexterEvent("sendToUserTrust",opponent,"playChess",pos);
			switch(decide(pos,1)){
				case WIN:
					clearData();
					Alert.show("恭喜你！你赢了！","本局结束",4,GameWindow.instance,closeHandler);
					sendDexterEvent("gobangWin");
					break;
				case DEUCE:
					clearData();
					Alert.show("和棋！","本局结束",4,GameWindow.instance,closeHandler);
					sendDexterEvent("gobangDeuce");
					break;
			}
		}
		private function closeHandler(event:CloseEvent):void{
			sendDexterEvent("clearChessBoard");
		}
		[DexterEvent]
		public function $playChess(pos:Object):void{
			var p:Point = new Point(pos.x,pos.y);
			grids[p.x][p.y] = -1;
			opponent.chess.push(p);
			sendDexterEvent("addChessman",p);
			switch(decide(p,-1)){
				case LOSE:
					clearData();
					Alert.show("对不起！你输了！","本局结束",4,GameWindow.instance,closeHandler);
					sendDexterEvent("gobangLose");
					break;
				case DEUCE:
					clearData();
					Alert.show("和棋！","本局结束",4,GameWindow.instance,closeHandler);
					sendDexterEvent("gobangDeuce");
					break;
				default:
					myTurn = true;
			}
		}
		private function decide(pos:Point,flag:int):String{
			var num:int = 0;
			var it:Point = pos.clone();
			//上下检测
			while(it.y>0 && grids[it.x][it.y] == flag){
				num ++;
				it.offset(0,-1);
			}
			it = pos.clone();
			it.offset(0,1);
			while(it.y<15 && grids[it.x][it.y] == flag){
				num ++;
				it.offset(0,1);
			}
			if(num>=5)return flag == 1?WIN:LOSE;
			//左右检测
			num = 0;
			it = pos.clone();
			while(it.x>0 && grids[it.x][it.y] == flag){
				num ++;
				it.offset(-1,0);
			}
			it = pos.clone();
			it.offset(1,0);
			while(it.x<15 && grids[it.x][it.y] == flag){
				num ++;
				it.offset(1,0);
			}
			if(num>=5)return flag == 1?WIN:LOSE;
			//撇检测
			num = 0;
			it = pos.clone();
			while(it.x<15&&it.y>0 && grids[it.x][it.y] == flag){
				num ++;
				it.offset(1,-1);
			}
			it = pos.clone();
			it.offset(-1,1);
			while(it.x>0&&it.y<15 && grids[it.x][it.y] == flag){
				num ++;
				it.offset(-1,1);
			}
			if(num>=5)return flag == 1?WIN:LOSE;
			//捺检测
			num = 0;
			it = pos.clone();
			while(it.x>0&&it.y>0 && grids[it.x][it.y] == flag){
				num ++;
				it.offset(-1,-1);
			}
			it = pos.clone();
			it.offset(1,1);
			while(it.x<15&&it.y<15 && grids[it.x][it.y] == flag){
				num ++;
				it.offset(1,1);
			}
			if(num>=5)return flag == 1?WIN:LOSE;
			//和棋检测
			if(opponent.chess.length == 113 || GUser.self.chess.length == 113)return DEUCE;
			return null;
		}
		[DexterEvent]
		public function begin(o:GUser):void{
			opponent = o;
			clearData();
			sendDexterEvent("clearChessBoard");
			myTurn = GUser.self.part == 0;
		}
	}
}